package com.sky.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonPrimitive;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 韩先楚
 */
@Service
public class SetMealService implements com.sky.service.SetMealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;
  @Autowired
  private StringRedisTemplate redisTemplate;
    @Autowired
    private ObjectMapper mapper;

    /**
     * 新增套餐
     */
    @Transactional
   public void addSetmeal(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(1);
        BeanUtils.copyProperties(setmealVO, setmeal);
        setmealMapper.addSetmeal(setmeal);
        //获取里面的菜品
        List<SetmealDish> dishes = setmealVO.getSetmealDishes();
         //获取桃餐中的id
        Long id = setmeal.getId();
        for (SetmealDish dish : dishes)
            dish.setSetmealId(id);

        //将与套餐绑定的菜品添加到数据库
        setmealMapper.addSetmealDish(dishes);
    }

    /**
     * 修改套餐状态
     */
    public void updateStatus(Integer id, Integer status) {
        setmealMapper.updateStatus(id, status);
    }

    /**
     * 根据id查询套餐
     */
    public SetmealVO getSetmealById(Integer id) {
        //先查询套餐表的数据
       SetmealVO setmealVO = setmealMapper.getSetmealById(id);
       //再根据返回的categoryId查询菜品表的数据
        Long categoryId = setmealVO.getCategoryId();
      String categoryName =  setmealMapper.getCateGoryName(categoryId);
      setmealVO.setCategoryName(categoryName);
        //再根据套餐id查询套餐菜品表的数据
        List<SetmealDish> dishes = setmealMapper.getSetmealDishes(id);
        setmealVO.setSetmealDishes(dishes);
        return setmealVO;
    }

    /**
     * 分页查询套餐
     */
    public PageResult page(Integer page, Integer pageSize, String name, Integer status, Integer categoryId) {
        PageHelper.startPage(page, pageSize);
        //查询套餐数据
        List<SetmealVO> setmealVOS = setmealMapper.page(name, status, categoryId);
        //查询总条数
        Page<SetmealVO> page1 = (Page<SetmealVO>) setmealVOS;
        return new PageResult(page1.getTotal(), page1.getResult());
    }

    /**
     * 批量删除套餐
     */
    @Transactional
    public void deleteSetmeal(List<Integer> ids) {
         //先删除与套餐绑定的菜品的数据
        if (ids!= null && ids.size() > 0) {
            setMealDishMapper.deleteBySetmealIds(ids);
            //再删除套餐数据
            setmealMapper.deleteSetmeal(ids);
        }
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) throws JsonProcessingException {
        List<Setmeal> list = setmealMapper.list(setmeal);
        //将查询到的菜品和口味数据加到redis中,并设置过期时间
        //将list转换为json字符串
        String json = mapper.writeValueAsString(list);
        redisTemplate.opsForValue().set("setMeal:"+setmeal.getCategoryId(),json,1000*60*2, TimeUnit.MINUTES);
        //设置过期时间为2分钟
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    /**
     * 修该套餐数据
     */
    @Transactional
    public void update(SetmealDTO setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        //先删除套餐餐菜品表的数据
       List<Integer> list = new ArrayList<>();
        long id = setmealDto.getId();
        list.add((int) id);
        setMealDishMapper.deleteBySetmealIds(list);
        //再查入新的菜品数据在套餐菜品表中
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        for (SetmealDish dish : dishes)
            dish.setSetmealId(id);
        setMealDishMapper.addSetmealDish(dishes);
        //再更新套餐表的数据
        setmealMapper.update(setmeal);


    }

}
